package com.freewater.parking.infrastructure.repository;

import com.freewater.parking.infrastructure.db.BlackListDao;
import com.freewater.parking.infrastructure.db.BlackListTable;
import com.freewater.parking.domain.blacklist.BlackList;
import com.freewater.parking.domain.blacklist.BlackListRepository;
import com.freewater.parking.domain.common.PlateNumber;
import org.springframework.stereotype.Repository;

import java.util.Optional;

@Repository
public class BlackListRepositoryImpl implements BlackListRepository {

    private final BlackListDao blackListDao;

    public BlackListRepositoryImpl(BlackListDao blackListDao) {
        this.blackListDao = blackListDao;
    }

    @Override
    public void save(BlackList blackList) {
        this.blackListDao.save(new BlackListTable(blackList));
    }

    @Override
    public Optional<BlackList> findByPlateNumber(PlateNumber plateNumber) {
        return this.blackListDao.findById(plateNumber.getValue())
                .map(blt -> new BlackList(PlateNumber.of(blt.getPlateNumber())));
    }

    @Override
    public void delete(BlackList blackList) {
        this.blackListDao.deleteById(blackList.getPlateNumber().getValue());
    }

    @Override
    public boolean existsById(PlateNumber plateNumber) {
        return this.blackListDao.existsById(plateNumber.getValue());
    }
}
